/**
 *
 */
package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.ehp.bp.bo.patient.PatientBO;
import com.naiterui.ehp.bp.bo.patient.PatientBaseBO;
import com.naiterui.ehp.bp.bo.patient.RequireReplyTemplateMsgBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientService;
import com.naiterui.ehp.bs.patient.utils.exception.PatientBusinessException;
import com.naiterui.ehp.bs.patient.vo.inner.InnerPatientVO;
import com.naiterui.ehp.bs.patient.vo.patient.PatientVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 患者信息
 * @author guoyongxiang
 * @since 2.11.0
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.USER_INFO})
@RequestMapping("ap/inner/patient")
@Controller
public class ApInnerPatientController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApInnerPatientController.class);

    @Autowired
    private IPatientService patientService;


    /**
     * 根据unionId查询患者信息
     * InnerPatientController.getPaientInfo()
     * @Author guoyongxiang
     * @Date 2017年9月19日
     * @since 2.11.0
     * @param unionId
     * @return
     */
    @RequestMapping("info")
    @ResponseBody
    public ResponseVO<InnerPatientVO> getPaientInfo(String unionId, Long patientId) {
        InnerPatientVO patientVO = patientService.getPatientById(unionId, patientId);
        return PublicService.returnResponseVO(patientVO);
    }

    /**
     * 获取患者用户信息
     * InnerPatientController.getBaseInfo()
     * @Author fangguanhong
     * @Date 2017年11月24日
     * @since 1.0.0
     * @param patientId
     * @return
     * @throws PatientBusinessException
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("base")
    public ResponseVO<PatientBO> getBaseInfo(Long patientId) throws PatientBusinessException {
        // 参数校验
        if (patientId == null) {
            LOGGER.error("inner patient base ERROR ; illegal param : patientId is null");
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }

        LOGGER.info("inner patient base SUCCESS ; patientId {}", patientId);
        PatientVO patientVo = patientService.getPatientBaseInfo(patientId);
        PatientBO patientBO = new PatientBO();
        BeanUtils.copyProperties(patientVo, patientBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientBO);
    }

    /**
     * 获取患者用户认证状态
     *
     * @param patientId
     *
     * @return
     *
     * @throws PatientBusinessException
     */
    @ResponseBody
    @RequestMapping("status")
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<Integer> getPatientStatus(Long patientId) throws PatientBusinessException {
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, 1);
    }

    /**
     * 医生针对患者求药暂停用药通知
     * @param requireReplyTemplateMsgBO
     * @return
     */
    @ResponseBody
    @PostMapping("/require/notice")
    public ResponseVO<Void> requireProcessNotice(@RequestBody RequireReplyTemplateMsgBO requireReplyTemplateMsgBO) {
        LOGGER.info("医生针对患者求药暂停用药模板消息通知，参数信息 {}", requireReplyTemplateMsgBO.toString());
        patientService.requireProcessNotice(requireReplyTemplateMsgBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 根据不同id获取患者信息
     *
     * @param unionId
     * @param miniOpenId
     * @param patientId
     * @return
     * @throws PatientBusinessException
     */
    @ResponseBody
    @GetMapping("/getPatientInfo")
    public ResponseVO getPatientInfo(String unionId, String miniOpenId, Long patientId) throws PatientBusinessException {
        return PublicService.returnResponseVO(patientService.getPatientInfo(unionId, miniOpenId, patientId));
    }

    /**
     * 根据unionId绑定微信小程序openId
     *
     * @param unionId
     * @param miniOpenId
     * @return
     * @throws PatientBusinessException
     */
    @ResponseBody
    @PostMapping("/bindMiniOpenId")
    public ResponseVO bindMiniOpenId(String unionId, String miniOpenId, String nickName, String avatar, Integer gender,
        String phone) throws PatientBusinessException {
        LOGGER.info("根据unionId绑定微信小程序openId, unionId {}, miniOpenId {}", unionId, miniOpenId);
        return PublicService.returnResponseVO(patientService.bindMiniOpenId(unionId, miniOpenId, nickName, avatar, gender, phone));
    }

    @GetMapping("getBaseInfoById")
    @ApiOperation("根据id获取患者基础信息")
    @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query")
    public ResponseVO<PatientBaseBO> getBaseInfoById(Long patientId) throws PatientBusinessException {
        // 参数校验
        if (patientId == null) {
            LOGGER.error("inner patient base ERROR ; illegal param : patientId is null");
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientService.getPatientBaseInfoById(patientId));
    }

    @ResponseBody
    @GetMapping("registPatientWithMiniapp")
    public ResponseVO<Void> registPatientWithMiniapp(String miniOpenId, String unionId) {
        LOGGER.info("小程序注册直接直接注册患者用户， miniOpenId {}, unionId {}", miniOpenId, unionId);
        patientService.addPatientWithMiniapp(miniOpenId, unionId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ResponseBody
    @GetMapping("getPatientIdByMiniOpenId")
    public ResponseVO<Long> getPatientIdByMiniOpenId(String miniOpenId) {
        LOGGER.info("根据消息miniopenid 获取患者id， miniOpenId：{}", miniOpenId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientService.getPatientIdByMiniOpenId(miniOpenId));
    }
}
